﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 合并文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {   
             Stream ms;
             string str;
             ofdSelectPic.Multiselect = true;
             if (ofdSelectPic.ShowDialog() == DialogResult.OK)
             {
                 if ((ms = ofdSelectPic.OpenFile()) != null)
                 {
                     str= ofdSelectPic.FileName;
                     textBox2.Text = textBox2.Text + str+"\r\n";
                     textBox3.Text = str;
                     ms.Position = 0;
                    ms.Close();
                 }
             }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择Txt所在文件夹";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
              

                string path= dialog.SelectedPath;
                textBox1.Text = path;
                var files = Directory.GetFiles(path, "*.txt");
                foreach (var file in files)
                {
                    textBox2.Text=textBox2.Text+file+ "\r\n";
                }
            }
        }
        public static void WriteToFile(string Path, string Text)
        {
            string content = File.ReadAllText(Path,Encoding.GetEncoding("gb2312"));
            content = Text + "\n" + content;
            File.WriteAllText(Path,content, Encoding.GetEncoding("gb2312"));
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string filename = textBox2.Text;
            string n = textBox4.Text;
            string[] filenamelist = filename.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string i in filenamelist) {
                string str = Path.GetFileNameWithoutExtension(i);
                WriteToFile(i, str);
            }
            FileCombine c = new FileCombine();
            c.CombineFile(filenamelist, n);
            MessageBox.Show("合并成功","提示");

            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Stream ms;
            string str;
            ofdSelectPic.Multiselect = true;
            if (ofdSelectPic.ShowDialog() == DialogResult.OK)
            {
                if ((ms = ofdSelectPic.OpenFile()) != null)
                {
                    str = ofdSelectPic.FileName;
                    textBox4.Text = str;
                    ms.Position = 0;
                    ms.Close();
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MaximizeBox = false;
            
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            AboutBox1 a = new AboutBox1();
            a.Show();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            使用说明 sm = new 使用说明();
            sm.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Regex rg = new Regex("(?<=第).*?[^0-9](?=章)", RegexOptions.Multiline | RegexOptions.Singleline);
            string ch_num;
            string num;
            string newfilename;
            string filename = textBox2.Text;
            string[] filenamelist = filename.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            int n = filenamelist.Length;
            for(int i=0;i<n; i++)
            {
                
                if (!rg.Match(filenamelist[i]).Value.Equals(""))
                {
         
                    ch_num = rg.Match(filenamelist[i]).Value;
                    num = 转换.ParseCnToInt(ch_num).ToString();
                    newfilename = filenamelist[i].Replace(ch_num, num);
                    if (File.Exists(filenamelist[i]))
                    {
                       
                        File.Move(filenamelist[i], newfilename);
                        filenamelist[i] = newfilename;
                    }
                }
            }
            textBox2.Text = "";
            foreach (string s in filenamelist) {
                textBox2.Text = textBox2.Text + s + "\r\n";
            }
            
        }

    
    }
} 
